/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        
        ListNode pre1,pre2;
        ListNode* cur1=&pre1,*cur2=&pre2;
        while(head){
            if(head->val<x){
                cur1->next=head;
                cur1=cur1->next;
            }
            else{
                cur2->next=head;
                cur2=cur2->next;
            }
            head=head->next;
        }
        cur1->next=pre2.next;
        cur2->next=nullptr;

        return pre1.next;
    }
};
